package com.db.manage.controller;

import com.db.manage.service.DriverService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/api/drivers")
@RequiredArgsConstructor
public class DriverController {
    private final DriverService driverService;

    @PostMapping("/upload")
    public ResponseEntity<String> uploadDriver(
            @RequestParam("file") MultipartFile file,
            @RequestParam("dbType") String dbType,
            @RequestParam("version") String version) {
        try {
            driverService.uploadDriver(file, dbType, version);
            return ResponseEntity.ok("驱动上传成功");
        } catch (IOException e) {
            return ResponseEntity.badRequest().body("驱动上传失败: " + e.getMessage());
        }
    }

    @GetMapping("/download")
    public ResponseEntity<String> downloadDriver(
            @RequestParam("dbType") String dbType,
            @RequestParam("version") String version,
            @RequestParam("driverName") String driverName) {
        try {
            driverService.downloadDriver(dbType, version, driverName);
            return ResponseEntity.ok("驱动下载成功");
        } catch (IOException e) {
            return ResponseEntity.badRequest().body("驱动下载失败: " + e.getMessage());
        }
    }
} 